# -*- coding: utf-8 -*-
import tkinter
from tkinter import *


def show_epuation(event):
    text = event.widget['text']
    entry.insert(END,text)

def  clear_entry(event):
    entry.delete(0,END)


def calculate(event):
    try:
        equation = entry.get()
        result = eval(equation)
        entry.insert(END,'='+str(result))
    except:
        showerror('输入错误')

a = Tk()
top = LabelFrame(padx = 10,pady = 10)
top.grid(row = 0, column= 1)
entry = Entry(top)
entry.grid(row = 1, columnspan = 6)

sevent = Button(top, text ="7")
sevent.grid(row = 2,column = 0,sticky=W+E+N+S,padx = 1,pady = 1)
sevent.bind('<Button>',func=show_epuation)

eight = Button(top, text ="8")
eight.grid(row = 2,column = 1,sticky=W+E+N+S,padx = 1,pady = 1)
eight.bind('<Button>',func=show_epuation)

nine = Button(top, text ="9")
nine.grid(row = 2,column = 2,sticky=W+E+N+S,padx = 1,pady = 1)
nine.bind('<Button>',func=show_epuation)

fuhao_1 = Button(top, text ="/")
fuhao_1.grid(row = 2,column = 3,sticky=W+E+N+S,padx = 1,pady = 1)
fuhao_1.bind('<Button>',func=show_epuation)

fuhao_2 =Button(top, text ="%")
fuhao_2.grid(row = 2,column = 4,sticky=W+E+N+S,padx = 1,pady = 1)
fuhao_2.bind('<Button>',func=show_epuation)

four = Button(top, text ="4")
four.grid(row = 3,column = 0,sticky=W+E+N+S,padx = 1,pady = 1)
four.bind('<Button>',func=show_epuation)

five = Button(top, text ="5")
five.grid(row = 3,column = 1,sticky=W+E+N+S,padx = 1,pady = 1)
five.bind('<Button>',func=show_epuation)

six = Button(top, text ="6")
six.grid(row = 3,column = 2,sticky=W+E+N+S,padx = 1,pady = 1)
six.bind('<Button>',func=show_epuation)

fuhao_3 = Button(top, text ="*")
fuhao_3.grid(row = 3,column = 3,sticky=W+E+N+S,padx = 1,pady = 1)
fuhao_3.bind('<Button>',func=show_epuation)

fuhao_4 = Button(top, text ="1/x")
fuhao_4.grid(row = 3,column = 4,sticky=W+E+N+S,padx = 1,pady = 1)

one = Button(top, text ="1")
one.grid(row = 4,column = 0,sticky=W+E+N+S,padx = 1,pady = 1)
one.bind('<Button>',func=show_epuation)

two = Button(top, text ="2")
two.grid(row = 4,column = 1,sticky=W+E+N+S,padx = 1,pady = 1)
two.bind('<Button>',func=show_epuation)

three = Button(top, text ="3")
three.grid(row = 4,column = 2,sticky=W+E+N+S,padx = 1,pady = 1)
three.bind('<Button>',func=show_epuation)

fuhao_5 = Button(top, text ="-")
fuhao_5.grid(row = 4,column = 3,sticky=W+E+N+S,padx = 1,pady = 1)
fuhao_5.bind('<Button>',func=show_epuation)

z = Button(top, text ="0")
z.grid(row = 5,column = 0,sticky=W+E+N+S,padx = 1,pady = 1)
z.bind('<Button>',func=show_epuation)

fuhao_6 = Button(top, text =".")
fuhao_6.grid(row = 5,column = 1,sticky=W+E+N+S,padx = 1,pady = 1)
fuhao_6.bind('<Button>',func=show_epuation)

C = Button(top, text ="C")
C.grid(row = 5,column = 2,sticky=W+E+N+S,padx = 1,pady = 1)
C.bind('<Button>',func=clear_entry)

fuaho_7 = Button(top, text ="+")
fuaho_7.grid(row = 5,column = 3,sticky=W+E+N+S,padx = 1,pady = 1)
fuaho_7.bind('<Button>',func=show_epuation)

fuaho_8 = Button(top, text ="=")
fuaho_8.grid(row = 4,rowspan = 2,column = 4,sticky=W+E+N+S,padx = 1,pady = 1)
fuaho_8.bind('<Button>',func=calculate)

mainloop()
